var easyUtils = {
	getQueryDiv :function(queryDiv){
		var param = {};
		$("#"+queryDiv).find('[data-qname]').each(function(){
			var key = $(this).data("qname"),
			val = $(this).val();
			param[key] = val;
		});
		return param;
	},
	getByContainer :function(container){
		var param = {};
		$("#"+container).find('[data-qname]').each(function(){
			var key = $(this).data("qname"),
			val = $(this).attr("title");
			param[key] = val;
		});
		return param;
	},
	// 包含分页下标第一页的参数
	getQueryDivDefault :function(queryDiv){
		var param = {};
    	$("#"+queryDiv).find('[data-qname]').each(function(){
    		var key = $(this).data("qname"),
    		    val = $(this).val();
    		param[key] = val;
    	});
    	param["page"] = 1;
    	return param;
	},
	setKeyValue:function(result){
		$("#inputDiv").data("userid",result["userid"]);
		$("#inputDiv").find('[data-qname]').each(function(){
			var key = $(this).data("qname"),
			val =  result[key];
			$(this).val(val);
		});
	},
	resetKeyValue:function(container, ignoreProp){//重置
		$("#"+container).find('[data-qname]').each(function(){
			var key = $(this).data("qname");
			// 不是忽略的属性，则重置其值
			if(key != ignoreProp){
				$(this).val("");
			}
		});
    },
    getData:function(param,callback){
		$.post(param.url,param.data,function(result){
			callback&&callback(result);
		}, "json");
	},
	buildTable : function(result,param,uiTable,con,pager,tbTemplate){
		var table = new uiTable({
        	"con": con||"#tbwrap",
            "data": {
                 "list": result.dataList,
                 "limit": result.limit,
                 "currentPage": result.currentPage
            },
            "template": tbTemplate || "tb_template",
            "pager": {
            	"con": pager||"#j_pager",
                "data": {
                	"currentPage": result.currentPage,
                    "pageCount": result.pageCount,
                    "rowCount": result.rowCount
                },
                template: '<a title="首页" class="page_first" data-role="first" ></a><a title="上一页" class="page_prev"  data-role="prev"></a><div data-value="pageNum"></div><a title="下一页" class="page_next"  data-role="next"></a><a title="尾页" class="page_last" data-role="last"></a> 共<span data-target="rowCount"></span>条 跳转到 <input type="text" data-target="input" class="page_input"/><a  data-role="go" class="go">GO</a>'
            }
        });

		//设置指定页数
		if (param.data["page"]) {
			table.getPager().setCurrentPage(param.data["page"]);
		}
        table.getPager().bind("changePage", function (event, page, hander) {
        	param.data.page = page;
        	easyUtils.getData(param, function (result) {
            	table.render({ "list": result.dataList, "limit": result.limit, "currentPage": result.currentPage});
                hander({"currentPage": result.currentPage, "pageCount": result.pageCount, "rowCount": result.rowCount });
         	});
        	
        });
	},
	buildTableCallback : function(result,param,uiTable,con,pager,callback){//如要加上回调就使用这个方法
		var con1=(typeof con==="string")?con:"#tbwrap";
		var pager1=(typeof pager==="string")?pager:"#j_pager";
		var callback=(typeof arguments[arguments.length-1]==="function")?arguments[arguments.length-1]:function(){};
		var table = new uiTable({
        	"con": con1||"#tbwrap",
            "data": {
                 "list": result.dataList,
                 "limit": result.limit,
                 "currentPage": result.currentPage
            },
            "template": "tb_template",
            "pager": {
            	"con": pager1||"#j_pager",
                "data": {
                	"currentPage": result.currentPage,
                    "pageCount": result.pageCount,
                    "rowCount": result.rowCount
                },
                template: '<a title="首页" class="page_first" data-role="first" ></a><a title="上一页" class="page_prev"  data-role="prev"></a><div data-value="pageNum"></div><a title="下一页" class="page_next"  data-role="next"></a><a title="尾页" class="page_last" data-role="last"></a> 共<span data-target="rowCount"></span>条 跳转到 <input type="text" data-target="input" class="page_input"/><a  data-role="go" class="go">GO</a>'
            }
        });
		var pager=table.getPager();
		pager.bind("changePage", function (event, page, hander) {
			param.data.page=page;
        	easyUtils.getData(param, function (result) {
            	table.render({ "list": result.dataList, "limit": result.limit, "currentPage": result.currentPage});
                hander({"currentPage": result.currentPage, "pageCount": result.pageCount, "rowCount": result.rowCount });
                pager.setCurrentPage(page);
                callback(result);
         	});
        	
        });
	},
	checkIE : function(){//检查浏览器
		var Sys = {};
        var ua = navigator.userAgent.toLowerCase();
        if (window.ActiveXObject) {//ie
           return '1'
		} else if (document.getBoxObjectFor) { //firfox
           return '2'
		} else if (window.MessageEvent && !document.getBoxObjectFor){ //chrome
           return '3'
		} else if (window.opera) { //opera
           return '4'
		} else if (window.openDatabase) { //safari
        	return '5'
		}
	},
	fullRows : function(con,tds) {//补空行  con:table的id，   tds:每行td的个数
		var len=$("#"+con).find("tr").length;
		var tdHtml = "";
		for (var i=0;i<tds;i++) {
			tdHtml +="<td>&nbsp;</td>";
		}
        for(var i=10;i>len;i--){
        	//$("<tr><td>&nbsp;</td><td>&nbsp;</td></tr>").appendTo($("#"+con));  
        	$("<tr>"+tdHtml+"</tr>").appendTo($("#"+con));  
        }
	},
	printParam : function(param) {//打印（alert）参数，调试时使用
		var s = "";
		for (var key in param) {
			s += key + "=" + param[key] + ";";
		}
		alert(s);
	},
	selectAll : function(containerId, flag) {
		$("input[type='checkbox']", $("#" + containerId)).each(function() {
			this.checked = flag;
		});
	},
	getCheckboxVal : function(containerId) {
		var result = "";
		$("input[type='checkbox']", $("#" + containerId)).each(function() {
			if (true==this.checked) {
				if ("on"!=this.value) {
					result += this.value + ";";
				}
			}
		});
		return result;
	},
	isNum : function(val) {
		var pattern = /^[0-9]+$/;
		return pattern.test(val);
	},
	isFloat : function(val) {//非负浮点数（正浮点数 + 0） 
		var pattern = /^\d+(\.\d+)?$/;
		return pattern.test(val);
	},
	eval : function(json) {
		return eval("("+json+")");
	},
	checkLen : function(val,len) {
		var length=val.length;
		for ( var i = 0; i < val.length; i++) {
			if (val.charCodeAt(i) > 127) {
				//length++;
				length+=1;
			}
		}
		return len >= length;
	},
	getPageIndex : function(j_pager) {
		return $(parent.document.getElementById(j_pager)).find(".page_input").val();
	}
};